import { PrinterManager } from './../../../utils/print/printer/printManager';
import { MessageBoxService, GrowlMessageType } from './../../../components/messageBox';
import { ConfigSvr } from './../../../services/config';
import { ExpeditionOrder } from './../../../models/expeditionOrder';
import { DeviceManager } from './../../../../../common/mpj-deviceCenter/src/deviceManager';
import { PrintComponentService } from './../printcontainer.component';
import { Table, Order } from './../../../../../model/clerkDataContext';
import { Component, OnInit, Output, EventEmitter, ChangeDetectorRef } from '@angular/core';

export class ExpeditionPrintService {
    static current: ExpeditionPrintService = new ExpeditionPrintService();

    private expeditionPrint: ExpeditionPrintComponent;

    regist(expeditionPrint: ExpeditionPrintComponent) {
        this.expeditionPrint = expeditionPrint;
    }

    clearData() {
        this.expeditionPrint.preOrder = null;
    }

    bindData(data: {
        store;
        preOrder;
        token;
        printers;
    }) {
        this.expeditionPrint.preOrder = data.preOrder;
        this.expeditionPrint.store = data.store;
        this.expeditionPrint.token = data.token;
        this.expeditionPrint.printers = data.printers;
        this.expeditionPrint.kitchenPrintSetting = data.store.setting && data.store.setting.receipt && data.store.setting.receipt.kitchen;
        if (data.printers && data.printers.length) {
            this.expeditionPrint.currentPrinter = data.printers[0];
        }
        this.expeditionPrint.dataBindFinish();
    }
}

@Component({
    selector: 'expedition-receipt',
    templateUrl: "./expeditionPrint.component.html",
    styleUrls: ["./../print.scss, ./../printer.component.scss"]
})
export class ExpeditionPrintComponent implements OnInit {
    constructor(
        private configSvr: ConfigSvr,
        private printerManager: PrinterManager,
        public ref: ChangeDetectorRef
    ) {
        ExpeditionPrintService.current.regist(this);
    }
    test = 1
    store: any = {};
    preOrder: ExpeditionOrder = null;
    token: string;
    printers: any = [];
    currentPrinter: any = {};
    adsServer;
    kitchenPrintSetting: string;
    @Output() createPrintTask: EventEmitter<any> = new EventEmitter<any>();

    ngOnInit() {

    }

    dataBindFinish() {
        this.ref.detectChanges();
        this.printTask();
    }

    printTask() {
        let that = this;
        let html = document.querySelector(".print-receipt");
        let tempClassName = "";
        this.adsServer = this.configSvr.adsAbout;
        if (!this.adsServer) {
            MessageBoxService.current.Growl(GrowlMessageType.Error, '', 'ads服务未配置');
            return;
        }
        if (this.printers && this.printers.length) {
            let tasks = [];
            for (let i = 0; i < this.printers.length; i++) {
                this.currentPrinter = this.printers[i];
                if (!this.currentPrinter.isUse || !this.currentPrinter.isConnect) {
                    continue;
                }
                let printId = this.currentPrinter.usage + "-" + 'expedition-order-' + (<any>this.preOrder).master + "-" + this.preOrder.id + "-" + this.preOrder.id + "/" + this.currentPrinter.id + "/" + this.preOrder.items[0].id + new Date().getTime();
                let outHtml = (<any>html).outerHTML.replace("print-receipt", "print-receipt " + this.currentPrinter.paperSize);
                let printParams = {
                    printMode: "http",
                    printTemplate: outHtml,
                    url: this.adsServer.url + "/printer/print/html",
                    mode: "text",
                    deviceInfo: this.printers[i].deviceInfo,
                    printId: printId,
                    type: this.printers[i].type,
                    storeId: this.store.id,
                    token: this.token,
                    directives: {
                        "auto-cut": this.printers[i].isCut,
                        "print-mode": "text",
                        "open-cash-drawer": false,
                        "paper-scroll-rows": this.printers[i].paperScrollRows.value,
                        "instruction-set": this.printers[i].deviceInfo.instructionSet
                    }
                }
                tasks.push(printParams);
            }
            that.createPrintTask.emit(tasks);
        } else {
            return;
        }

    }
}